package sisi.enquires;

import java.util.List;

import javax.persistence.EntityManager;

/**
 * NotesuppController.java - The controller class for the <i>notesupp database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 20/3/2014
 * 
 */
public class NotesuppController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Notesupp[] get(Integer idSupplier) {
		List<Notesupp> lista = getList(idSupplier);
			return lista.toArray(new Notesupp[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Notesupp> getList(Integer idSupplier) {

		String cWhere = "";
		cWhere = " WHERE c.suppid = "+idSupplier;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Notesupp as c " + cWhere 
					+ " order by c.idmaster ");

			List<Notesupp> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	public Notesupp getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Notesupp notesuppx = em.find(Notesupp.class, id);
			return notesuppx;
		} finally {
			em.close();
		}
	}
	
	public Notesupp getxIdReqEIdSupplier(int idReq, int idSupplier) {
		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Notesupp as c WHERE c.id = " + idReq + " AND c.suppid = " + idSupplier );

			Notesupp notesupp = new Notesupp();
			if (q.getResultList().size()==1){
				notesupp = (Notesupp)q.getSingleResult();
			}
			return notesupp;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Notesupp add(Notesupp notesupp) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(notesupp);
			em.getTransaction().commit();
			notesupp = refresh(notesupp);
		} finally {
			em.close();
			return notesupp;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Notesupp notesupp) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Notesupp notesuppx = em.find(Notesupp.class, notesupp.getIdmaster());
			notesuppx.setId(notesupp.getId());
			notesuppx.setIdmaster(notesupp.getIdmaster());
			notesuppx.setNotes(notesupp.getNotes());
			notesuppx.setSuppid(notesupp.getSuppid());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Notesupp refresh(Notesupp notesupp) {

		Notesupp notesuppx = notesupp;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			notesuppx = em.find(Notesupp.class, notesupp.getIdmaster());
			em.flush();
			em.refresh(notesuppx);
		} finally {
			em.close();
			return notesuppx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Notesupp notesupp) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Notesupp notesuppx = em.find(Notesupp.class, notesupp.getIdmaster());
			em.remove(notesuppx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
